<?php
/*
 */
class paging {

	// 当前页码
	var $currendnum  = 0;
	// 页面总数
	var $pagecount   = 0;
	// 数据总数
	var $dataCount   = 0;
	// 页面数据范围
	var $pageSize    = 10;
	// 分页间隔范围
	var $pageNumSize = 10;
	// 跳转连接文本需要替换
	var $linkText    = "";

	public function setCurrendnum($currendnum){
		$this ->currendnum = $currendnum;
		//echo $this ->currendnum."|" ;
	}
	
	public function setPageSize($pageSize){
		$this ->pageSize = $pageSize;
		//echo $this ->pageSize."|" ;
	}
	
	public function setPageNumSize($pageNumSize){
		$this ->pageNumSize = $pageNumSize;
		//echo $this ->pageNumSize."|" ;
	}
	
	public function setDataCount($dataCount){
		$this ->dataCount = $dataCount;
		//echo $this ->dataCount."|" ;
	}
	
	public function setlinkText($linkText){
		$this ->linkText = $linkText;
	}

	public function makePageText(){
		// 分页处理开始。。。
		if($this ->dataCount == 0 || $this ->currendnum == 0){
			return 0;
		}
		$pagecountdata=array();

		// 页码计算
		if($this ->dataCount%$this->pageSize == 0){
			$this->pagecount =        $this-> dataCount/$this->pageSize;
		}else{
			$this->pagecount = floor( $this-> dataCount/$this->pageSize) + 1 ;
		}
		
		// 数据总数
		$attributeArray = array();
		$attributeArray["style"] = "class1";
		$pagecountdata[count($pagecountdata)] = $this->makeAttributeHtmlTag("em",$this ->dataCount,$attributeArray);

		// 计算页码范围
		$startPageNum = floor($this->currendnum / $this->pageNumSize)*$this->pageNumSize;
		if($this->currendnum % $this->pageNumSize == 0){
			$startPageNum = ($this->currendnum / $this->pageNumSize - 1) * $this->pageNumSize;
		}
		$endPageNum    = $startPageNum + $this->pageNumSize;
		if( $this->pagecount < $startPageNum + $this->pageNumSize){
			$endPageNum = $this->pagecount;
		}

		// 前10页
		if( $this->currendnum > $this->pageNumSize ){
			$pagecountdata[count($pagecountdata)] = $this->makeLink($startPageNum+1-$this->pageNumSize,"...");
		}

		// 前翻页
		if($this->currendnum > 1){
			$pagecountdata[count($pagecountdata)] =$this->makeLink($this->currendnum-1,"<<");
		}

		// 数字页
		for($i = $startPageNum ; $i < $endPageNum  ; $i++) {
				
			// 当前页码用文字表示
			if($this->currendnum == $i+1){
				$pagecountdata[count($pagecountdata)] = $this->makeHtmlTag("strong",$i+1);
				continue;
			}
			// 当前页码以外用连接表示
			$pagecountdata[count($pagecountdata)] = $this->makeLink($i+1,$i+1);
		}
		// 后翻页
		if($this->currendnum < $this->pagecount){
			$pagecountdata[count($pagecountdata)] =$this->makeLink($this->currendnum+1,">>");
		}

		// 后10页   页码总数  > 当前页面+页码表示尺寸
		if( $startPageNum + $this->pageNumSize < $this->pagecount ){
			$pagecountdata[count($pagecountdata)] = $this->makeLink($startPageNum+1+$this->pageNumSize,"...");
		}
		
		return $pagecountdata;
	}
	
	/*
		名称：makeLink
		功能：超级链接
		备注：
	*/
	function makeLink($pageNumArg,$linkText){
		$tempLinkText = str_replace("!!PageNum!!",$pageNumArg,$this-> linkText);
		$tempLinkText = str_replace("!!LinkText!!",$linkText,$tempLinkText);
		return $tempLinkText ;
	}
	
	/*
		名称：makeHtmlTag
		功能：标签生成
		备注：
	*/
	function makeHtmlTag($tagName,$tagText){
		$retHtmlStr  = "<".$tagName;
		$retHtmlStr .= ">";
		$retHtmlStr .= $tagText;
		$retHtmlStr .= "</".$tagName.">";
		return $retHtmlStr ;
	}
	
	/*
		名称：makeAttributeHtmlTag
		功能：标签生成
		备注：
	*/
	function makeAttributeHtmlTag($tagName,$tagText,$attributeArray){
		$retHtmlStr  = "<".$tagName;
		foreach($attributeArray as $k=>$a){
			$retHtmlStr  .= " ".$k."='".$a."' " ;
		}
		$retHtmlStr .= ">";
		$retHtmlStr .= $tagText;
		$retHtmlStr .= "</".$tagName.">";
		return $retHtmlStr ;
	}
}
?>
